package ranab.server.ftp.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.tree.TreePath;

import ranab.gui.GuiUtils;
import ranab.server.ftp.FtpUser;
import ranab.server.ftp.FtpConfig;

/**
 * This panel displays all the logged in users.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>.
 */
public 
class FtpConnectionPanel extends PluginPanel {
    
    private JTable mjConnectionTable;
    private FtpConfig mConfig;   
    private FtpConnectionTableModel mModel;
    
    /**
     * Instantiate login panel.
     */
    public FtpConnectionPanel(FtpTree tree) {
        super(tree);
        mModel = new FtpConnectionTableModel();
        initComponents();
    }
     
    /**
     * Initialize UI components
     */
    private void initComponents() {
        
        setLayout(new BorderLayout());
        
        mjConnectionTable = new JTable(mModel);
        mjConnectionTable.setPreferredScrollableViewportSize(new Dimension(470, 320));
        mjConnectionTable.setColumnSelectionAllowed(false);
        JScrollPane bottomPane = new JScrollPane(mjConnectionTable, 
                                     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);                          
        add(bottomPane, BorderLayout.CENTER);
        
        
        // buttons
        JPanel btnPane = new JPanel();
        btnPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        
        JButton jLogoutBtn = new JButton("Disconnect");
        btnPane.add(jLogoutBtn);
        
        JButton jSpyBtn = new JButton("Spy User");
        btnPane.add(jSpyBtn);
        
        add(btnPane, BorderLayout.SOUTH);
        
        // event handlers
        jLogoutBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                closeConnection();
             }
        });
        
        jSpyBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                spyUser();
             }
        });
    }
   
     
    /**
     * Logout this user
     */
    private void closeConnection() {
        int indices[] = mjConnectionTable.getSelectedRows();
        if(indices.length == 0) {
            GuiUtils.showErrorMessage(getTree().getRootPanel(), "Please select connection(s).");
            return;
        }
        
        boolean response = GuiUtils.getConfirmation(getTree().getRootPanel(), "Do you really want to close the selected connection(s)?");
        if(!response) {
            return;
        }
        
        for(int i=indices.length; --i>=0; ) {
            FtpUser user = mModel.getUser(indices[i]);
            if(user != null) {
                mConfig.getConnectionService().closeConnection(user.getSessionId());
            }
        }
    }
    
    /**
     * Monitor the selected user.
     */
    private void spyUser() {
        int indices[] = mjConnectionTable.getSelectedRows();
        if(indices.length == 0) {
            GuiUtils.showErrorMessage(getTree().getRootPanel(), "Please select connection(s).");
            return;
        }
        
        // monitor all the selected users
        for(int i=indices.length; --i>=0; ) {   
            FtpUser thisUser = mModel.getUser(indices[i]);
            if (thisUser != null) {
                FtpSpyContainerPanel spyPanel = (FtpSpyContainerPanel)getTree().getPluginPanel("Spy");
                spyPanel.monitorConnection(thisUser); 
            }
        }
        
        // select tree spy node
        Object[] spyPath = new Object[] {
            getTree().getRoot(), "Spy"
        };
        getTree().setSelectionPath(new TreePath(spyPath));        
        
    }
     
    /**
     * Refresh window.
     */
    public void refresh(FtpConfig cfg) {
        mConfig = cfg;
        if (cfg != null) {
            mModel.refresh(mConfig);
        }
    } 
        
    /**
     * Is displayable
     */    
    public boolean isDisplayable() {
        return mConfig != null;
    }
        
}